//
//  CZMainViewController.swift
//  SinaSwift
//
//  Created by I Do Believe on 15/11/23.
//  Copyright © 2015年 apple11. All rights reserved.
//

import UIKit

class CZMainViewController: UITabBarController {

    ///撰写按钮的点击事件
    func composeClick() {
        //__FUNCTION__打印方法名称
        print(__FUNCTION__)
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //设置自定义的tabBar
//        let newTabBar = CZTabBar()
//        tabBar = CZTabBar()//只读，不能赋值
        //利用KVC
//        setValue(newTabBar, forKey: "tabBar")
//        
//        newTabBar.composeButton.addTarget(self, action: "composeClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        tabBar.tintColor = UIColor.orangeColor()
        
        //首页
        let homeVC = CZHomeViewController()
//        homeVC.title = "首页"
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        addChildViewController(UINavigationController(rootViewController: homeVC))
          addChildViewController(homeVC, title: "首页", imageName: "tabbar_home")
        
        //消息
        let msgVC = CZMsgViewController()
//        msgVC.title = "消息"
//        msgVC.tabBarItem.image = UIImage(named: "tabbar_message_center")
//        addChildViewController(UINavigationController(rootViewController: msgVC))
            addChildViewController(msgVC, title: "消息", imageName: "tabbar_message_center")
        
        //占位
        addChildViewController(UIViewController())
        
        //发现
        let disVC = CZProViewController()
//        disVC.title = "发现"
//        disVC.tabBarItem.image = UIImage(named: "tabbar_discover")
//        addChildViewController(UINavigationController(rootViewController: disVC))
addChildViewController(disVC, title: "发现", imageName: "tabbar_discover")
        
        //我
        let profileVC = CZProViewController()
//        profileVC.title = "我"
//        profileVC.tabBarItem.image = UIImage(named: "tabbar_profile")
//        addChildViewController(UINavigationController(rootViewController: profileVC))
        addChildViewController(profileVC, title: "我", imageName: "tabbar_profile")
        
        //在这里添加撰写按钮太早了,会被占位item给挡住了
        //设置撰写按钮的frame
        let width = tabBar.bounds.size.width / 5
        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        composeButton.frame = frame
        //        //尝试能否通过带到最前面的方法来实现覆盖,这个方法也必须在viewWillAppear后面实现
        //        tabBar.bringSubviewToFront(composeButton)
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        
        //设置撰写按钮的frame
        //        let width = tabBar.bounds.size.width / 5
        //        let frame = CGRect(x: 2 * width - 5, y: 0, width: width + 10, height: tabBar.bounds.height)
        //        composeButton.frame = frame
        tabBar.bringSubviewToFront(composeButton)
    }
    
    /**
    自定义添加tabbar子控制器
    
    - parameter controller: 子控制器
    - parameter title:      子控制器的标题
    - parameter imageName:  子控制器的图片名称
    */
    // private 表示只有本类或当前文件的其他类可以访问
    private func addChildViewController(controller: UIViewController, title:String, imageName:String) {
        controller.title = title
        controller.tabBarItem.image = UIImage(named: imageName)
        
        //拼接高亮图片名称
        let highlightedImageName = imageName + "_highlighted"
        //让选中的图片使用用来的颜色，不用被系统渲染
        let highlightedImage = UIImage(named: highlightedImageName)?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        
        
        //设置高亮图片
//        controller.tabBarItem.selectedImage = highlightedImage
        //设置标题颜色
//        controller.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orangeColor()], forState: UIControlState.Highlighted)
        
        addChildViewController(UINavigationController(rootViewController: controller))
    }
    
    //懒加载
    //撰写按钮
    lazy var composeButton: UIButton = {
        let button = UIButton( )
        //设置参数
        //设置按钮背景图片
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), forState: UIControlState.Highlighted)
        
        //设置按钮的图片
        button.setImage(UIImage(named: "tabbar_compose_icon_add"), forState: UIControlState.Normal)
        button.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), forState: UIControlState.Highlighted)
        
        //添加到tabBar里面
        self.tabBar.addSubview(button)
        
        return button
    }( )
    
    

}
